myApp.controller('barcode', ['$scope', '$http', function($scope, $http){

	$scope.goodsID = "";
	$scope.rule = "EAN-13";
	$scope.countryCode = "";
	$scope.manufacturerCode = "";
	$scope.barcodes = [];

	var allColor = $scope.top_Color;
	var allSize = $scope.top_Size;
	var allStyle = $scope.top_Style;

	$scope.searchBarcodes = function() {
		if ($scope.goodsID === "" || $scope.goodsID === null) {
			alert("请填写商品ID！");
			return;
		}

		$scope.barcodes = [];
		
		var data = {"goodsId": $scope.goodsID};
		var postCfg = {
			headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
			transformRequest: function (data) {
				return $.param(data);
			}
		};

		$http.post('searchBarcodeByGoodsID', data, postCfg).then(function successCallback(response) {
			
			if(response.data[0].Nothing === "Nothing") {
				alert("该商品还没有条形码！");
			}else {
				var barcodesJson = response.data;
				for (var i = 0; i < barcodesJson.length; i++) {
					var barcode = {};
					barcode.colorName = findObjByID(barcodesJson[i].colorId, allColor, "colorId").name;
					barcode.sizeName = findObjByID(barcodesJson[i].sizeId, allSize, "sizeId").name;
					barcode.styleName = findObjByID(barcodesJson[i].styleId, allStyle, "styleId").name;
					barcode.code = barcodesJson[i].code;
					$scope.barcodes.push(barcode);
				}
			}

			console.log("ok：" + response);
		}, function errorCallback(response) {
			alert("条形码生成失败！");
			console.log("fail：" + response);
		});

	};

	$scope.createBarcodes = function() {

		if ($scope.goodsID === "" || $scope.goodsID === null) {
			alert("请填写商品ID！");
			return;
		}else if($scope.countryCode === "") {
			alert("请填写国家代码！");
			return;
		}else if($scope.manufacturerCode === "") {
			alert("请填写厂商识别码！");
			return;
		}else if($scope.countryCode.length !== 3) {
			alert("只能填写3位国家代码！");
			return;
		}else if($scope.manufacturerCode.length !== 5) {
			alert("只能填写5位厂商识别码！");
			return;
		}

		$scope.barcodes = [];

		var data = {"goodsId": $scope.goodsID, "rule": $scope.rule, "countryCode": $scope.countryCode, "manufacturerCode": $scope.manufacturerCode};
		var postCfg = {
			headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
			transformRequest: function (data) {
				return $.param(data);
			}
		};

		$http.post('addBarcode', data, postCfg).then(function successCallback(response) {
			
			if(response.data[0].Nothing === "Nothing") {
				alert("该商品不存在！");
			}else {
				var barcodesJson = response.data;
				for (var i = 0; i < barcodesJson.length; i++) {
					var barcode = {};
					barcode.colorName = findObjByID(barcodesJson[i].colorId, allColor, "colorId").name;
					barcode.sizeName = findObjByID(barcodesJson[i].sizeId, allSize, "sizeId").name;
					barcode.styleName = findObjByID(barcodesJson[i].styleId, allStyle, "styleId").name;
					barcode.code = barcodesJson[i].code;
					$scope.barcodes.push(barcode);
				}
			}

			console.log("ok：" + response);
		}, function errorCallback(response) {
			alert("条形码生成失败！");
			console.log("fail：" + response);
		});

	};

	function findObjByID(Id, allObjs, IdName) {
		for (var i = 0; i < allObjs.length; i++) {
			if(Id == allObjs[i][IdName]) {
				return allObjs[i];
			}
		}
		return {name: "无"};
	}

}]);